// api.huhuiyu.top后端访问 Authorization
const BASE_URL = 'https://api.huhuiyu.top'
const TOKEN_KEY = BASE_URL + '_token_key'

let tokenInfo = {
	save: (data) => {
		if (data && data.token) {
			uni.setStorageSync(TOKEN_KEY, data.token)
		}
	},
	load: () => {
		let token = uni.getStorageInfoSync(TOKEN_KEY)
		return token ? token : ""
	},
	remove: () => {
		uni.removeStorageSync(TOKEN_KEY)
	}
}

let api = {
	send: (url, param, method, cb) => {
		uni.request({
			url: BASE_URL + url,
			data: param,
			method: method,
			header: {
				Authorization: tokenInfo.load(),
				'content-type': 'application/json'
			},
			success: (resp) => {
				tokenInfo.save(resp.data)
				cb(resp.data)
			},
			fail: (err) => {
				cb({
					success: false,
					code: 500,
					message: "你的应用出bug了，赶紧加班修复"
				})
			}
		})
	},
	get: (url, param, cb) => {
		api.send(url, param, "GET", cb)
	},
	post: (url, param, cb) => {
		api.send(url, param, "POST", cb)
	},
	delete: (url, param, cb) => {
		api.send(url, param, "DELETE", cb)
	},
	put: (url, param, cb) => {
		api.send(url, param, "PUT", cb)
	},
	patch: (url, param, cb) => {
		api.send(url, param, "PATCH", cb)
	},
}

export default api
export {
	api as api
}